package com.huaxinkai.mobile.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/hello")
public class 	helloController {
	private static Logger log = LoggerFactory.getLogger(helloController.class);
	
	@RequestMapping("/hello/world.htm")
	public String helloMvc() {
		System.out.println("Hellow world");
		return "index";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/goUpload.htm")
	public String goUpload(){
		log.debug("/hello/goUpload");
		return "do_upload";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="/doUpload.htm",method=RequestMethod.POST)	
	public String doUpload(@RequestParam("file") MultipartFile file,Model model,HttpServletRequest request){
		System.out.println("----/hello/doUpload-----");
		
		try {
			String path = request.getSession().getServletContext().getRealPath("/dynamic/new_");
			if(!file.isEmpty()){
				File ff =  new File(path + file.getOriginalFilename());
				System.out.println("file path:"+ff.getPath());
				FileUtils.copyInputStreamToFile(file.getInputStream(), ff);
				model.addAttribute("src", "/resources/dynamic/new_"+file.getOriginalFilename());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "success";
	}
	@RequestMapping(value="/show.htm")
	public String showImg(Model model, HttpServletRequest req){
		System.out.println("----/hello/show-----");
		String path = req.getServletContext().getRealPath("");
		File file = new File("");
		file = new File(path+"/dynamic/images");
		List<String> list = new ArrayList<String>();
		
		if(file != null && file.list().length > 0){
			for (String str : file.list()) {
				list.add(str);
			}
		}
		
		model.addAttribute("SRC", list);
		return "show_img";
	}
	@RequestMapping(value="/showGirl.htm")
	public String showGirl(Model model, HttpServletRequest req){
		System.out.println("----/hello/showGirl-----");
		String path = req.getServletContext().getRealPath("");
		File file = new File("");
		file = new File(path+"/dynamic/girls");
		List<String> list = new ArrayList<String>();
		if(file != null && file.list().length > 0){
			for (String str : file.list()) {
				list.add(str);
			}
		}
		model.addAttribute("SRC", list);
		return "showGirl";
	}
	@RequestMapping(value="/showLogo.htm")
	public String showLogo(Model model, HttpServletRequest req){
		System.out.println("----/hello/showLogo-----");
		String path = req.getServletContext().getRealPath("");
		File file = new File("");
		file = new File(path+"/dynamic/logo");
		List<String> list = new ArrayList<String>();
		if(file != null && file.list().length > 0){
			for (String str : file.list()) {
				list.add(str);
			}
		}
		model.addAttribute("SRC", list);
		return "showLogo";
	}
	@RequestMapping(value="/showUpload.htm")
	public String showUpload(Model model, HttpServletRequest req){
		System.out.println("----/hello/showUpload-----");
		String path = req.getServletContext().getRealPath("");
		File file = new File("");
		file = new File( path +"/dynamic/upload");
		List<String> list = new ArrayList<String>();
		if(file != null && file.list().length > 0){
			for (String str : file.list()) {
				list.add(str);
			}
		}
		model.addAttribute("SRC", list);
		return "showUpload";
	}
	
}
